﻿#include "PauseLayer.h"
#include "GameUtil.h"
#include "MainGameScene.h"
#include "StartGameScene.h"

using namespace cocos2d::extension;

PauseLayer::~PauseLayer(void)
{
}

bool PauseLayer::init()
{
	if (!CCLayer::init()) {
		return false;  
	}  
	_winSize = CCDirector::sharedDirector()->getWinSize(); 
	
	m_pUiLayer = UILayer::create();
    addChild(m_pUiLayer);
        
    m_pWidget = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("MyGame_05_1.json"));
    m_pUiLayer->addWidget(m_pWidget);
			
	// panel
	UIPanel *root = dynamic_cast<UIPanel*>(m_pUiLayer->getWidgetByName("root"));
	root->setPosition(ccp(_winSize.width/2, _winSize.height/2));
	root->setAnchorPoint(ccp(0.5f, 0.5f));

	UIButton* button_01 = dynamic_cast<UIButton*>(m_pUiLayer->getWidgetByName("button_01"));
    button_01->addReleaseEvent(this, coco_releaseselector(PauseLayer::resume));

    UIButton* button_02 = dynamic_cast<UIButton*>(m_pUiLayer->getWidgetByName("button_02"));
    button_02->addReleaseEvent(this, coco_releaseselector(PauseLayer::restart));
	
    UIButton* button_03 = dynamic_cast<UIButton*>(m_pUiLayer->getWidgetByName("button_03"));
    button_03->addReleaseEvent(this, coco_releaseselector(PauseLayer::mainmenu));

	UIImageView* image_share = dynamic_cast<UIImageView*>(m_pUiLayer->getWidgetByName("image_share"));
    image_share->addReleaseEvent(this, coco_releaseselector(PauseLayer::share));

	slider01 = dynamic_cast<UISlider*>(m_pUiLayer->getWidgetByName("slider_01"));
	slider01->addEventListener(this, sliderpercentchangedselector(PauseLayer::bgVolumeChange));

	slider02 = dynamic_cast<UISlider*>(m_pUiLayer->getWidgetByName("slider_02"));
	slider02->addEventListener(this, sliderpercentchangedselector(PauseLayer::effectVolumeChange));

	updateVolume();

	return true;  
} 

void PauseLayer::bgVolumeChange(CCObject *pSender, SliderEventType type)
{
	switch (type)
    {
        case cocos2d::extension::SLIDER_PERCENTCHANGED:
        {
            GameUtil::instance()->getSetting()->setBgVolume(slider01->getPercent());
			updateVolume();
        }
            break;
            
        default:
            break;
    }
}


void PauseLayer::effectVolumeChange(CCObject *pSender, SliderEventType type)
{
	switch (type)
    {
        case cocos2d::extension::SLIDER_PERCENTCHANGED:
        {
            GameUtil::instance()->getSetting()->setEffectVolume(slider02->getPercent());
			updateVolume();
        }
            break;
            
        default:
            break;
    }
}


void PauseLayer::restart(CCObject* pSender)
{
	this->removeFromParent();
	GameUtil::instance()->getWaveManager()->startWave();
    CCDirector::sharedDirector()->replaceScene(MainGameScene::scene());
}

void PauseLayer::mainmenu(CCObject* pSender)
{
	CCDirector::sharedDirector()->replaceScene(StartGameScene::scene());
}

void PauseLayer::share(CCObject* pSender)
{
	saveScreen();
}

void PauseLayer::saveScreen()
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCRenderTexture* screen = CCRenderTexture::create(size.width, size.height);
	CCScene* temp = CCDirector::sharedDirector()->getRunningScene();
	screen->begin();
	temp->visit();
	screen->end();
	screen->saveToFile("cocos2d-x-screenshot.png", kCCImageFormatPNG);
	CC_SAFE_DELETE(screen);
}

void PauseLayer::resume(CCObject* pSender)
{
	this->removeFromParent();
	CCLayer* gameLayer = (CCLayer *)GameUtil::instance()->getGameScene()->getChildren()->objectAtIndex(0);
	GameUtil::resume(gameLayer);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
	CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeAllEffects();
}

void PauseLayer::paused(CCScene* scene)
{
	_scene = scene;
	CCLayer* gameLayer = (CCLayer *)scene->getChildren()->objectAtIndex(0);
	GameUtil::pause(gameLayer);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
	CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseAllEffects();

	scene->addChild(this);

	this->setScale(.0f);
	this->runAction(CCScaleTo::create(1.0f, 1.0f));
}

void PauseLayer::updateVolume()
{
	int bgVolume = GameUtil::instance()->getSetting()->getBgVolume();
	int effectVolume = GameUtil::instance()->getSetting()->getEffectVolume();

	slider01->setPercent(bgVolume);
	slider02->setPercent(effectVolume);

	UILabelAtlas* v1 = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("volume_01"));
	char c[10];
	sprintf(c, "%d", bgVolume);
	v1->setStringValue(c);

	UILabelAtlas* v2 = dynamic_cast<UILabelAtlas*>(m_pUiLayer->getWidgetByName("volume_02"));
	sprintf(c, "%d", effectVolume);
	v2->setStringValue(c);

	GameUtil::instance()->updateVolume();
}